import {
  createPageData,
  deletePageData,
  editPageData,
  getPageListData
} from '@/service/main/system/system'
import { IRootState } from '@/store/type'
import { ElMessage } from 'element-plus'
import { Module } from 'vuex'
import { ISystemSate } from './types'

const systemModule: Module<ISystemSate, IRootState> = {
  namespaced: true,
  state() {
    return {
      usersList: [],
      usersCount: 0,
      roleList: [],
      roleCount: 0,
      departmentList: [],
      departmentCount: 0,
      goodsList: [],
      goodsCount: 0,
      menuList: [],
      menuCount: 0,
      categoryList: [],
      categoryCount: 0
    }
  },
  mutations: {
    CHANGE_USERS_LIST(state, userList: any[]) {
      state.usersList = userList
    },
    CHANGE_USERS_COUNT(state, userCount: number) {
      state.usersCount = userCount
    },
    CHANGE_ROLE_LIST(state, roleList: any[]) {
      state.roleList = roleList
    },
    CHANGE_ROLE_COUNT(state, roleCount: number) {
      state.roleCount = roleCount
    },
    CHANGE_DEPARTMENT_LIST(state, departmentList: any[]) {
      state.departmentList = departmentList
    },
    CHANGE_DEPARTMENT_COUNT(state, departmentCount: number) {
      state.departmentCount = departmentCount
    },
    CHANGE_GOODS_LIST(state, goodsList: any[]) {
      state.goodsList = goodsList
    },
    CHANGE_GOODS_COUNT(state, goodsCount: number) {
      state.goodsCount = goodsCount
    },
    CHANGE_MENU_LIST(state, menuList: any[]) {
      state.menuList = menuList
    },
    CHANGE_MENU_COUNT(state, menuCount: number) {
      state.menuCount = menuCount
    },
    CHANGE_CATEGORY_LIST(state, categoryList: any[]) {
      state.categoryList = categoryList
    },
    CHANGE_CATEGORY_COUNT(state, categoryCount: number) {
      state.categoryCount = categoryCount
    }
  },
  actions: {
    // 获取列表数据
    async getPageListAction({ commit }, payload: any) {
      // 1.获取pageUrl
      const pageName = payload.pageName
      const pageUrl = `/${pageName}/list`

      // 2.对页面发送请求
      const pageResult = await getPageListData(pageUrl, payload.queryInfo)

      // 3.将数据存储到state中
      const { list, totalCount } = pageResult.data

      // const changePageName = pageName.toUpperCase()
      commit(`CHANGE_${pageName.toUpperCase()}_LIST`, list)
      commit(`CHANGE_${pageName.toUpperCase()}_COUNT`, totalCount)
    },

    // 删除表格数据
    async deletePageDataAction({ dispatch }, payload: any) {
      const pageUrl = `/${payload.pageName}/${payload.id}`

      // 调用删除函数
      const deletResult = await deletePageData(pageUrl)

      // 删除操作结果提示
      if (deletResult.code === 0) {
        ElMessage({
          message: deletResult.data,
          type: 'success'
        })
      } else {
        ElMessage.error(deletResult.data)
      }
      // 重新请求列表数据
      dispatch('getPageListAction', {
        pageName: payload.pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    },

    // 新建数据
    async createPageDataAction({ dispatch }, payload: any) {
      const { pageName, newData } = payload
      const pageUrl = `/${pageName}`
      const createPageDataResult = await createPageData(pageUrl, newData)
      // 操作提示
      if (createPageDataResult.code === 0) {
        ElMessage({
          message: createPageDataResult.data,
          type: 'success'
        })
      } else {
        ElMessage.error(createPageDataResult.data)
      }
      // 重新请求列表数据
      dispatch('getPageListAction', {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    },

    // 编辑数据
    async editPageDataAction({ dispatch }, payload: any) {
      const { pageName, editData, id } = payload
      const pageUrl = `/${pageName}/${id}`
      const editPageDataResult = await editPageData(pageUrl, editData)
      console.log(editPageDataResult, 'editPageDataResult')

      // 操作提示
      if (editPageDataResult.code === 0) {
        ElMessage({
          message: editPageDataResult.data,
          type: 'success'
        })
      } else {
        ElMessage.error(editPageDataResult.data)
      }
      // 重新请求数据
      dispatch('getPageListAction', {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    }
  },
  getters: {
    pageListData(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}List`]
      }
    },
    pageListCount(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}Count`]
      }
    }
  }
}

export default systemModule
